Test Failed
Pull Request — master (#2)
by Yo
01:40
created

index.js ➔ ???   C

Complexity

Conditions 9
Paths 18

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 41.0835

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
c 1
b 0
f 0
nc 18
nop 1
dl 0
loc 64
rs 6.5449
ccs 17
cts 64
cp 0.2656
crap 41.0835

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ... ➔ ??? 0 8 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"use strict";
2
3 2
const requestPromise = require('request-promise-native');
4 2
const pkg = require('../../package.json');
5 2
const logger = require('../logger/taskLogger')('Client');
6 2
const Response = require('../model/Response');
7
8 2
const defaultOptionList = {
9
    headers: {'User-Agent': pkg.name},
10
    json: true,
11
    simple: false,
12
    resolveWithFullResponse: true
13
};
14
15
/**
16
 * @param {Request} request
17
 *
18
 * @returns {Promise<Response|Error>}
19
 */
20 2
module.exports = (request) => {
21
22
    const optionList = Object.assign(
23
        {},
24
        defaultOptionList,
25
        {
26
            uri: request.getUri(),
27
            method: request.getMethod(),
28
            headers: Object.assign({}, defaultOptionList.headers, request.getHeaders()),
29
            json: request.isJson()
30
        }
31
    );
32
33 7
    if (request.getForm() && Object.keys(request.getForm()).length > 0) {
34 1
        optionList.form = request.getForm();
35
    }
36 7
    if (request.getPayload() && Object.keys(request.getPayload()).length > 0) {
37 1
        optionList.body = request.getPayload();
38
    } else {
39 6
        optionList.body = optionList.json === true ? {} : '';
40
    }
41 7
    if (request.getQueryString() && Object.keys(request.getQueryString()).length > 0) {
42 1
        optionList.qs = request.getQueryString();
43
    } else {
44 6
        optionList.qs = optionList.json === true ? {} : null;
45
    }
46
47 7
    const transformResponseToResponseObject = response => {
48
        return new Response(
49
            response.body,
50
            response.statusCode,
51
            response.headers,
52
            request
53
        );
54
    };
55
56 7
    const transformErrorToResponseObject = error => {
57
        logger.debug('[Error] Conversion to Response.', {options: optionList});
58
59 2
        return Promise.resolve(new Response(
60
            optionList.json === true ? {
61
                error: error.error.code,
62
                message: error.message
63
            } : `${error.error.code} ${error.message}`,
64
            500,
65
            {},
66
            request
67
        ));
68
    };
69
70 7
    const logResponseObject = response => {
71
        logger.debug('[Reponse]', {response: response, options: optionList});
72
73 7
        return response;
74
    };
75
76 7
    logger.debug('[Request]', {options: optionList});
77
78 7
    const transform = requestPromise(optionList)
79
        .then(transformResponseToResponseObject, transformErrorToResponseObject);
80
81 7
    return transform
82
        .then(logResponseObject);
83
};
84